package com.autocar.reader.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.autocar.reader.R;
import com.autocar.reader.bean.SupplySubBean;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.util.UIHelper;
import com.org.any.autocar.ProductSupplyActivity;
import com.org.any.autocar.fragment.IopenSupply;

public class ProductDemandAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private ArrayList<SupplySubBean> listBeans;
	private Bitmap bmp;
	private IopenSupply iopensupply;
	private Context context = null;

	public ProductDemandAdapter(Context context,IopenSupply iopensupply) {
		inflater = LayoutInflater.from(context);
		this.context = context;
		bmp = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.no_img_0);
		this.iopensupply = iopensupply;
	}

	public void setListSupplySubBean(ArrayList<SupplySubBean> listBeans) {
		this.listBeans = listBeans;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listBeans == null ? 0 : listBeans.size();
	}

	@Override
	public SupplySubBean getItem(int position) {
		return listBeans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler viewHodler;
		if (convertView == null) {
			viewHodler = new ViewHodler();
			convertView = inflater.inflate(R.layout.product_demand_item, null);
			viewHodler.picIv = (ImageView) convertView
					.findViewById(R.id.product_demand_item_iv_pic);
			viewHodler.nameTv = (TextView) convertView
					.findViewById(R.id.product_demand_item_tv_name);
			viewHodler.demandLl = (LinearLayout) convertView
					.findViewById(R.id.product_demand_item_demand_ll);
			viewHodler.msgTv = (TextView) convertView
					.findViewById(R.id.product_demand_item_tv_demand);
			viewHodler.describeTv = (TextView) convertView
					.findViewById(R.id.product_demand_item_tv_describe);
			viewHodler.supplyLl = (LinearLayout) convertView
					.findViewById(R.id.product_demand_item_supply_ll);
			viewHodler.supplyTv = (TextView) convertView
					.findViewById(R.id.product_demand_item_tv_supply);
			viewHodler.timeTv = (TextView) convertView
					.findViewById(R.id.product_demand_item_tv_time);
			convertView.setTag(viewHodler);
		} else {
			viewHodler = (ViewHodler) convertView.getTag();
		}
		
		
		final SupplySubBean subean= getItem(position);
		
		viewHodler.nameTv.setText(subean.getUname());
		viewHodler.timeTv.setText(subean.getAddtime());
		
		ImageUtils.fb.display(viewHodler.picIv, subean.getPhoto(),bmp);
		
		if(subean.getSupply()==null || subean.getSupply().equals("")){
			viewHodler.supplyLl.setVisibility(View.GONE);
			viewHodler.supplyTv.setText("");
		}else{
			viewHodler.supplyLl.setVisibility(View.VISIBLE);
			viewHodler.supplyTv.setText(subean.getSupply());
		}
		
		if(subean.getMsg()==null || subean.getMsg().equals("")){
			viewHodler.demandLl.setVisibility(View.GONE);
			viewHodler.msgTv.setText("");
		}else{
			viewHodler.demandLl.setVisibility(View.VISIBLE);
			viewHodler.msgTv.setText(subean.getMsg());
		}
		
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				iopensupply.opentSupply(subean);
			}
		});
		viewHodler.picIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				UIHelper.toAuthorInfo(context, subean.getUid());
			}
		});
		return convertView;
	}

	class ViewHodler {
		ImageView picIv;
		TextView nameTv;
		TextView describeTv;
		TextView supplyTv;
		TextView msgTv;
		TextView timeTv;
		LinearLayout supplyLl;
		LinearLayout demandLl;
	}

}
